package com.leonzx.base.controller.userpermission;

import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.Menu;
import com.leonzx.base.service.system.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-11 15:29
 */
@Controller
@RequestMapping("/menu/")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/tomenu")
    @RequiresPermissions("/menu/tomenu")
    public String tomenu() {
        return "userpermission/menu/menu";
    }

    /**
     * 跳转到增加节点页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/addequal")
    @RequiresPermissions("/menu/tomenu")
    public String addequal(String id, Model model) {
        Menu menu = new Menu();
        menu.setId(id);
        String parentId = "";
        if (StringUtils.isNotBlank(id)){
            if (!Objects.isNull(menuService.findParentMenu(menu))) {
                String parentName = menuService.findParentMenu(menu).getName();
                parentId = menuService.findParentMenu(menu).getId();
                menu.setParentName(parentName);
                menu.setParentId(parentId);
                model.addAttribute("menu", menu);
            }
        }
        model.addAttribute("parentId", parentId);
        return "userpermission/menu/add";
    }

    /**
     * 跳转到增加子节点
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/addson")
    @RequiresPermissions("/menu/tomenu")
    public String addson(String id, Model model) {
        Menu menu = menuService.findById(id);
        String parentName = menu.getName();
        String parentId = menu.getId();
        menu.setParentName(parentName);
        menu.setParentId(parentId);
        model.addAttribute("menu", menu);
        model.addAttribute("parentId", parentId);
        return "userpermission/menu/add";
    }

    /**
     * 加载信息到编辑动态html
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    @RequiresPermissions("/menu/tomenu")
    public String edit(String id, Model model) {
        Menu menu = menuService.findById(id);
        String parentId = menu.getParentId();
        if (StringUtils.isNotBlank(parentId)) {
            String parentName = menuService.findById(parentId).getName();
            menu.setParentName(parentName);
        }
        model.addAttribute("menu", menu);
        return "userpermission/menu/detail";
    }

    /**
     * 个人自定义菜单
     *@Author chen
     *@Date 2018/7/2 10:40
     */
    @RequestMapping("/customMenu")
    public String customMenu() {
        return "userpermission/menu/customMenu";
    }

}
